const ajax = require('../../../utils/ajax/ajax.js')
Page({
    data: {
        vipList: [],
        shopList: [],
        vipLoading: true,
        shopLoading: true,
        pages: 1
    },
    props: {
        tabIndex: 0
    },
    onLoad: function () {
        this.getVipList()
        this.getSpaceRecList()
        console.log(this.props.tabIndex);
    },
    onTabClick(e) {
        const tabIndex = e.detail.index
        this.props.tabIndex = tabIndex
    },
    getVipList() {
        const that = this
        ajax.post({
            url: '/wx/getMemberRec',
            data: {
                page: 1,
                limit: 10
            },
            success(res) {
                that.setData({
                    vipList: res.data.list
                })
            }
        })
    },
    getSpaceRecList() {
        const that = this
        ajax.post({
            url: '/wx/getSpaceRecList',
            data: {
                page: 1,
                limit: 10
            },
            success(res) {
                that.setData({
                    shopList: res.data.list
                })
            }
        })
    },

    onReachBottom () {
        const tabIndex = this.props.tabIndex
        console.log(tabIndex);
        if (tabIndex === 0) {
          (this.data.pages) ++;
          this.reachBottomVip()
          return
        }
    
        if (tabIndex === 1) {
          (this.data.pages) ++;
          this.reachBottomSpaceRec()
          return
        }
      },
    
    reachBottomVip() {
        const that = this
        ajax.post({
            url: '/wx/getMemberRec',
            data: {
                page: that.data.pages,
                limit: 10
            },
            success(res) {
                let vipList = res.data.list
                if (vipList.length === 0) {
                    that.setData({
                        vipLoading: true
                    })
                    return
                }
                
                if (that.data.pages === 1) {
                    that.setData({
                        vipList: vipList
                    })
                    return
                }

                if (that.data.pages !== 1) {
                    let newList = that.data.vipList.concat(vipList)
                    that.setData({
                        vipList: newList
                    })
                    return
                }
            }
        })
    },

    reachBottomSpaceRec() {
        const that = this
        ajax.post({
            url: '/wx/getSpaceRecList',
            data: {
                page: that.data.pages,
                limit: 10
            },
            success(res) {
                let shopList = res.data.list
                if (shopList.length === 0) {
                    that.setData({
                        shopLoading: true
                    })
                    return
                }
                
                if (that.data.pages === 1) {
                    that.setData({
                        shopList: shopList
                    })
                    return
                }

                if (that.data.pages !== 1) {
                    let newList = that.data.shopList.concat(shopList)
                    that.setData({
                        shopList: newList
                    })
                    return
                }

            }
        })
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.getVipList()
        this.getSpaceRecList()
        wx.stopPullDownRefresh()
    }
})